//
//  RadioPlayViewController.m
//  Leisure
//
//  Created by Alex on 16/7/5.
//  Copyright © 2016年 蓝鸥科技有限公司西安分公司. All rights reserved.
//

#import "RadioPlayViewController.h"

@interface RadioPlayViewController ()<UITableViewDataSource,UITableViewDelegate,playManagerDelegate>

@property (weak, nonatomic) IBOutlet UISlider *sliderA;
@property (weak, nonatomic) IBOutlet UIButton *kuaijin;
@property (weak, nonatomic) IBOutlet UIButton *kuaitui;
@property (weak, nonatomic) IBOutlet UIButton *zanting;
@property (weak, nonatomic) IBOutlet UIImageView *imageV;
@property (weak, nonatomic) IBOutlet UITableView *tableView;
@property (weak, nonatomic) IBOutlet UIWebView *webView;
@property (nonatomic,assign)NSInteger currentIndex; // 当前下标
@property (nonatomic,strong)UIView *greenView;
@end

@implementation RadioPlayViewController

// 根据下标找到对应的cell
- (UITableViewCell *)fetchCellWithIndex:(NSInteger)index {
    
    return [self.tableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:index inSection:0]];
}

- (UIView *)greenView {
    
    if (!_greenView) {
        _greenView = [[UIView alloc]initWithFrame:CGRectMake(kWidth - 10, 0, 10, [self fetchCellWithIndex:0].frame.size.height)];
        _greenView.backgroundColor = [UIColor greenColor];
    }
    return _greenView;
}

- (void)setDataSource:(NSArray *)dataSource {
    
    if (_dataSource[0] != dataSource[0]) {
        self.currentIndex = -1;
        _dataSource = dataSource;
    }
}

+ (instancetype)shardManager {
    
    static RadioPlayViewController *handle = nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        handle = [[RadioPlayViewController alloc]initWithNibName:@"RadioPlayViewController" bundle:nil];
    });
    return handle;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
 //   self.automaticallyAdjustsScrollViewInsets = NO;
    
    self.edgesForExtendedLayout = UIRectEdgeNone;
    
    [self.tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"cell"];
    
    self.currentIndex = -1;
    self.sliderA = 0;
    [PlayManager sharedManager].delegate = self;
    // 设置webView
    self.webView.backgroundColor = [UIColor clearColor];
    self.webView.opaque = NO;
}

// 滑块
- (IBAction)slider:(UISlider *)sender {
    
    [[PlayManager sharedManager]playMusicWithSliderValue:sender.value];
    
}

// 上一首
- (IBAction)kuaijin:(UIButton *)sender {
    
    if (self.currentIndex == 0) {
        
        self.currentIndex = self.dataSource.count - 1;
    } else {
    self.currentIndex--;
    }
    [self setPlayInfoWithIndex:self.currentIndex];
}

// 下一首
- (IBAction)kuaitui:(UIButton *)sender {
    
    if ((self.currentIndex = self.dataSource.count - 1)) {
        
        self.currentIndex = 0;
    }else {
    self.currentIndex++;
    }
    [self setPlayInfoWithIndex:self.currentIndex];
}

// 暂停
- (IBAction)zanting:(UIButton *)sender {
    
    BOOL result = [[PlayManager sharedManager]isPlaying];
    if (!result) {
        [[PlayManager sharedManager]playMusic];
    } else {
        [[PlayManager sharedManager]pauseMusic];
    }
    
}

- (void)setPlayInfoWithIndex:(NSInteger)index {
    
    RadioDetailModel *model = self.dataSource[index];
    
    [self.webView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:model.webview_url]]];
    // 子线程
    dispatch_async(dispatch_get_global_queue(0, 0), ^{
        
        // 播放音乐
        [[PlayManager sharedManager]prepareTopPlayMusicWithUrl:model.musicUrl];
        
       dispatch_async(dispatch_get_main_queue(), ^{
           
           self.title = model.title;
           [self.greenView removeFromSuperview];
           [[self fetchCellWithIndex:index] addSubview:self.greenView];
           [self.tableView reloadData];

       });
    });
}

// 视图将要出现
- (void)viewWillAppear:(BOOL)animated {
    
    [super viewWillAppear:animated];
    
    // 为了防止点同一首歌重新播放的情况发生
    if (self.index != self.currentIndex) {
        
        self.currentIndex = self.index;
        // 设置信息
        [self setPlayInfoWithIndex:self.currentIndex];
    }
}

#pragma  mark -----------------TableViewDelegate---------------

- (NSInteger )tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
    return self.dataSource.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell" forIndexPath:indexPath];
    
    cell.textLabel.text = [self.dataSource[indexPath.row] title];
    
    return cell;
}

#pragma mark  --------playManagerDelegate--------
- (void)playManagerDelegateFetchTotalTime:(NSString *)totalTimer currentTime:(NSString *)currentTime progress:(CGFloat)progress {
    
    self.sliderA.value = progress;
    
}

- (void)playToNextMusic {
    
   
    [self kuaitui:nil];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    
}



@end
